package com.jinsp.cameralibrary.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

/**
 * Created by luoshuai on 1/10/22
 * <p>
 * Bitmap工具
 */
public class BitmapUtils {

    /**
     * 虚化bitmap
     *
     * @param context   上下文
     * @param srcBitmap 原始位图
     * @param radius    虚化程度 0 - 25
     * @return 虚化后的bitmap
     */
    public static Bitmap startBlur(Context context, Bitmap srcBitmap, int radius) {
        if (context != null && srcBitmap != null) {
            RenderScript renderScript = RenderScript.create(context);
            Allocation srcBitmapAllocation = Allocation.createFromBitmap(renderScript, srcBitmap);
            Allocation resultBitmapAllocation = Allocation.createTyped(renderScript, srcBitmapAllocation.getType());
            ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
            scriptIntrinsicBlur.setInput(srcBitmapAllocation);
            scriptIntrinsicBlur.setRadius(radius);
            scriptIntrinsicBlur.forEach(resultBitmapAllocation);
            resultBitmapAllocation.copyTo(srcBitmap);
            renderScript.destroy();
            return srcBitmap;
        }
        return null;
    }

    /**
     * 转换bitmap
     *
     * @param srcBitmap 目标bitmap
     * @param degree    旋转的角度
     * @return 旋转后的bitmap
     */
    public static Bitmap rotateBitmap(Bitmap srcBitmap, int degree) {
        if (srcBitmap != null) {
            int width = srcBitmap.getWidth();
            int height = srcBitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.setRotate(degree);
            Bitmap newBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, width, height, matrix, false);
            srcBitmap.recycle();
            return newBitmap;
        }
        return null;
    }

    /**
     * 不读取内存进行等比例缩放文件中的位图
     *
     * @param context 上下文
     * @param id      图片资源id
     * @param width   缩放后的宽度
     * @param height  缩放后的高度
     * @return 缩放后的位图
     */
    public static Bitmap scaleBitmap(Context context, int id, String filePath, int width, int height) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        if (filePath == null) {
            //从资源文件中获取位图
            BitmapFactory.decodeResource(context.getResources(), id, options);
        } else {
            //从本地文件中获取位图
            BitmapFactory.decodeFile(filePath, options);
        }
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        if (srcWidth > width && srcHeight > height) {
            int scaleWidth = srcWidth / width;
            int scaleHeight = srcHeight / height;
            options.inSampleSize = Math.max(scaleHeight, scaleWidth);
        }
        options.inJustDecodeBounds = false;
        if (filePath == null) {
            return BitmapFactory.decodeResource(context.getResources(), id, options);
        }
        return BitmapFactory.decodeFile(filePath, options);
    }
}
